package Model.MarketPlace;

import Model.Planet.Planet;
import java.util.Random;


/**
 * class holds the information for a marketplace
 **/
/**
 * @author Waseem
 *
 */
public class MarketPlaceModel {
	/**
	 * 
	 */
	private Planet planet;

	/**
	 * constructor
	 * @param p
	 **/
	public MarketPlaceModel(Planet p){
		this.planet = p;
	}

	
	/**
	 * @param item
	 * @param p
	 * @return
	 */
	public static double updatedPrice(Item item, Planet p){
		final Random rand = new Random();
		double price = (item.getPrice() + item.getIpl() * 
		        (p.getTechLevel().getLevel() - item.getMinTechProduce()));
		if(rand.nextDouble() > .5){
			price = price + (price * (rand.nextInt(item.getVariance()) * .01));
		}
		else{
			price = price - (price * (rand.nextInt(item.getVariance()) * .01));
		}
		
		if(item.getIe() == p.getIe()){
			price = price * (2 + rand.nextDouble());
		}
		if(item.getCr() == p.getCr()){
			price = price * .5;
		}
		if(item.getEr() == p.getResources()){
			price = price * 2.0;
		}
		
		return Math.abs(price);
	}

	/**
	 * updates quantity of item at planet
	 * @param item
	 * @param p
	 **/
	public static int updateQuantity(Item item, Planet p){
		final double quantity = item.getQuantity() + (item.getQuantity() * .15) - 
		        Math.abs((item.getTtp() - p.getTechLevel().getLevel()));
		return (int) Math.round(quantity);
	}

	/**
	 * gets the marketplace items from the planet
	 * @return itemList
	 **/
	public MarketPlaceItem[] getItems(){
		return planet.getItemList();
	}

	/**
	 * setter for planet
	 * @param p
	 **/
	public void setPlanet(Planet p){
		this.planet = p;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString(){
		return super.toString();
	}
}
